jQuery.jrMaskPanel ={
	maskDiv:null,	
	show:function(props){
		if(!props){
			props={};
		}
		
		if(jQuery.jrMaskPanel.maskDiv===null){
			jQuery.jrMaskPanel.createMaskDiv();
		}
		var maskDiv=jQuery.jrMaskPanel.maskDiv;
		
		if(props.opacity!==null){
			jQuery.jrMaskPanel.maskDiv.opacity(props.opacity);
		}
		if(props.zIndex!==null){
		   jQuery.jrMaskPanel.maskDiv.css("z-index",props.zIndex);
		}
		jQuery.jrMaskPanel.maskDiv.show();
	},
	hide:function(){
		jQuery.jrMaskPanel.maskDiv.hide();
	},
	createMaskDiv:function(){
		jQuery("<div id='_mask_panel' style='display:none'></div>").appendTo("body");
  		jQuery.jrMaskPanel.maskDiv=jQuery("#_mask_panel");
  		jQuery.jrMaskPanel.maskDiv.css("position","absolute");
        jQuery.jrMaskPanel.maskDiv.css("left","0");
        jQuery.jrMaskPanel.maskDiv.css("top","0");
        jQuery.jrMaskPanel.maskDiv.css("z-index","1");
        jQuery.jrMaskPanel.maskDiv.css("background","#000");  
        jQuery.jrMaskPanel.maskDiv.css("width" ,"100%"); 
        jQuery.jrMaskPanel.maskDiv.css("height","100%");
        jQuery.jrMaskPanel.maskDiv.css("filter","alpha(opacity=30)");
        jQuery.jrMaskPanel.maskDiv.opacity(30);
	}
};

function JrWindowPanel(){
	this.id=null;
	this.title=null;
	this.container=null;
	this.submitButton=null;
	this.concelButton=null;
	this.head=null;
	this.content=null;
	this.uri=null;
	this.show=function(){
	   this.container.Draggable({
            handle:"#"+this.id+" > div, .head"
        });
       this.container.show();
	}
	this.hide=function(){
	   this.container.DraggableDestroy();
	   this.container.hide();
	}
}


jQuery.jrWindow={
	windows:new Array(),
	show:function(props){
		if(!props || props===null){
			props={};
		}
		var wp = null;
		if(!props.id){		
			 wp= jQuery.jrWindow.build(props);	
		}
		else{
			if(jQuery.jrWindow.windows[props.id]){
				wp=jQuery.jrWindow.windows[props.id];
			}else{
				wp=jQuery.jrWindow.build(props);	
			}
		}
		wp.container.Draggable({
            handle:"#"+wp.id+" > div, .head"
        });
		wp.container.show();
		return wp;
	},
	getOrCreate:function(props){
	   if(!props || props===null){
            props={};
        }
        var wp = null;
        if(!props.id){      
             wp= jQuery.jrWindow.build(props);  
        }
        else{
            if(jQuery.jrWindow.windows[props.id]){
                wp=jQuery.jrWindow.windows[props.id];
                if(props.content){
                	wp.content.html(props.content);
                }
            }else{
                wp=jQuery.jrWindow.build(props);    
            }
        }
        return wp;
	},
	build:function(props){	    
		var wp=new JrWindowPanel();
		if(props.id){
			wp.id=props.id;
		}else{
			wp.id="_jr_window_"+new Date().getTime();
		}
		var title="";
		if(props.title){
			title=props.title;
		}		
		
		wp.container=jQuery("<div id='"+jQuery.jrWindow.id+"' class='window'></div>").appendTo("body");
		
		
		
		wp.head=jQuery("<div class='head'><span>"+title+"</span></div>");
		wp.content=jQuery("<div class='content'>......</div>");
		if(props.content){
			wp.content.html(props.content);
		}
		wp.close=jQuery("<div class='close'></div>");
		wp.head.appendTo(wp.container);
		
		wp.close.appendTo(wp.head);
		wp.close.bind("click", function(){
  			wp.container.hide();
		}); 
		wp.content.appendTo(wp.container);

		if(props.content){
			wp.content.html(props.content);
		}else if(props.uri){
			wp.uri=props.uri;
		}
		//set css
		if(props.width){
			 wp.container.css('width',props.width);
		}
		if(props.height){
			 wp.content.css('height',props.height);
		}
		
		//add button
		if(props.confirm&&props.confirm==true){
			var footer=jQuery("<div style='text-align:center'><button class='button'>确认</button>&nbsp;<button class='button'>取消</button></div>");
			var eb1=footer.children('button')[0];			 
			var eb2=footer.children('button')[1];
			wp.submitButton=jQuery(eb1);
			wp.concelButton=jQuery(eb2);
			wp.concelButton.bind("click", function(){
	  			wp.container.hide();
			}); 
			if(props.callback)
			{	
				var args=props.args;
				var c=function(){
					props.callback(args);
				}
				wp.submitButton.bind("click", c); 
			}
			//wp.submitButton.appendTo(footer);
			//wp.concelButton.appendTo(footer);
			footer.appendTo(wp.container);
		}
		
		wp.container.toCenter();
				
		jQuery.jrWindow.windows[wp.id]=wp;
		return wp;
	}
};

jQuery.extend({
  mask: jQuery.jrMaskPanel.show,
  window: jQuery.jrWindow.getOrCreate
}); 


//********** extend jquery functions ****** 
jQuery.fn.extend({	
	/*
	 * 
	 */
   opacity: function(value) {
    return this.each(function() {
    	var e=jQuery(this);
    	
    	if(jQuery.browser.msie){
  		    e.css("filter","alpha(opacity=30)"); 
  		}else{
  		    e.css("opacity","."+value); 
  		}
    });
  },
   /*
    * 
    */
   toCenter: function() {
    return this.each(function() {
    	var e=jQuery(this);
    	var bh=jQuery("body").height();
		var bw=jQuery("body").width();
		var eh=e.height();
		var ew=e.width();
		var top=bh/2-eh/2;
		var left=bw/2-ew/2;
		if(top<100) {top=100;}
		e.css("position","absolute");
    	e.css("top",top);
		e.css("left",left);
    });
  },
  /*
   * 
   */
  flash: function(color) {
   return this.each(function() {
   	 var e=jQuery(this);
   	 var originalBackgound=e.css("background"); 
 
   	 e.css({background: color||'red'}); 
   	 e.fadeIn("slow",function(){ e.css({background:originalBackgound}); } ); 
   });
  } ,
   setUri:function(uri){
		  return this.each(function() {			      
			  	 var e=jQuery(this);			  	 
			  	 e.data("uri",uri);
			});
	},
   reload:function(url,data,callback){		  
	   return this.each(function() {			   
		     var e=jQuery(this);
		     if(url&&url!=null){
		    	 e.data("uri",url);
		     }
		     var uri=e.data("uri");
		      
		     e.load(uri,data,callback);
		});
   }
}); 
